package me.aurora.repository.spec;

import me.aurora.domain.SysLog;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

public class LogSpec implements Specification<SysLog> {

    private String username;
    private String method;
    private String operation;
    private String location;

    public LogSpec(String username, String method, String operation,String location) {
       this.username = username;
       this.method = method;
       this.operation = operation;
       this.location = location;
    }

    @Override
    public Predicate toPredicate(Root<SysLog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
        List<Predicate> list = new ArrayList<Predicate>();


        if (null!=username) {
            list.add(cb.like(root.get("username").as(String.class), "%" + username + "%" ));
        }

        if (null!=method) {
            list.add(cb.like(root.get("method").as(String.class), "%" + method + "%" ));
        }

        if (null!=operation) {
            list.add(cb.like(root.get("operation").as(String.class), "%" + operation + "%" ));
        }

        if (null!=location) {
            list.add(cb.like(root.get("location").as(String.class), "%" + location + "%" ));
        }

        Predicate[] p = new Predicate[list.size()];
        return cb.and(list.toArray(p));
    }
}
